<?php


namespace App\Http\Controllers\admin\service;


use App\Http\Controllers\utils\LogUtil;
use App\Http\Controllers\utils\ToolUtil;
use App\Http\Controllers\utils\UploadUtil;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\admin\model\AdminModel;

class FileService
{
    /**
     * 增加附件文件
     * @param $param
     * @return false|string
     */
    public static function addFile($param){

        $uid = (int)$param['userId'];
        $admin = AdminModel::queryFirst(['id'=>$uid],['group_id']);
        if(empty($admin) || !ToolUtil::checkIsAdmin($admin['group_id'])){
            return ToolUtil::returnJson(400,'没有权限');
        }

        $param['remark'] = "增加附件文件";
        $param['details'] = ToolUtil::handleSuccessOrError($param);

        $type = (int)$param['type'];
        if(DB::table('t_file')->where(['type'=>$type])->count('id')){
            return ToolUtil::returnJson(301,'已经存在这个类型附件文件');
        }
        $typeArr = ToolUtil::getFileTypeArray();
        if(!array_key_exists($type,$typeArr)){
            return ToolUtil::returnJson(300,'类型错误');
        }

        $data = [
            'type'=>$type,
            'file'=>$param['file'],
            'admin_id'=>$param['userId'],
            'add_time'=>time()
        ];
        if(DB::table('t_file')->insert($data)){
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 修改附件文件
     * @param $param
     * @return false|string
     */
    public static function editFile($param){

        $uid = (int)$param['userId'];
        $admin = AdminModel::queryFirst(['id'=>$uid],['group_id']);
        if(empty($admin) || !ToolUtil::checkIsAdmin($admin['group_id'])){
            return ToolUtil::returnJson(400,'没有权限');
        }

        $id = (int)$param['id'];

        $type = (int)$param['type'];
        if(DB::table('t_file')->where(['type'=>$type])->where('id', '<>', $id)->count('id')){
            return ToolUtil::returnJson(301,'已经存在这个类型附件文件');
        }

        $typeArr = ToolUtil::getFileTypeArray();

        if(!array_key_exists($type,$typeArr)){
            return ToolUtil::returnJson(300,'类型错误');
        }

        $param['remark'] = "修改附件文件";
        $param['details'] = ToolUtil::handleSuccessOrError($param);
        $data = [
            'type'=>$type,
            'file'=>$param['file'],
            'update_time'=>time()
        ];
        if(DB::table('t_file')->where('id',$id)->update($data)){
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }


    /**
     * 获取附件文件
     * @param $param
     * @return false|string
     */
    public static function queryFile($param){
        $count = DB::table('t_file')->count('id');
        $page = ToolUtil::calculationPage($param,$count);
        $data = DB::table('t_file as b')
            ->leftJoin('t_admin as a','b.admin_id','=','a.id')
            ->offset($page['startSize'])
            ->limit($page['pageSize'])
            ->get(['b.id','a.username','b.type','b.file','b.add_time','b.update_time'])->toArray();
        if(!empty($data)){

            foreach ($data as $key => $value) {
                $data[$key]['type_name'] = ToolUtil::getFileTypeName($value['type']);
            }

            return ToolUtil::returnJson(200,'成功',$data,$count);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编号获取附件文件
     * @param $param
     * @return false|string
     */
    public static function queryIdFile($param){
        $data = DB::table('t_file')->where('id',$param['id'])->first(['id','type','file']);
        if($data){
            $data['type_name'] = ToolUtil::getFileTypeName($data['type']);
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 获取附件文件类型
     * @param $param
     * @return false|string
     */
    public static function getFileType($param){

        $uid = (int)$param['userId'];
        $admin = AdminModel::queryFirst(['id'=>$uid],['group_id']);
        if(empty($admin) || !ToolUtil::checkIsAdmin($admin['group_id'])){
            return ToolUtil::returnJson(400,'没有权限');
        }

        $data = ToolUtil::getFileType();
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }


    /**
     * 删除附件文件
     * @param $param
     * @return false|string
     */
    public static function deleteFile($param){

        $uid = (int)$param['userId'];
        $admin = AdminModel::queryFirst(['id'=>$uid],['group_id']);
        if(empty($admin) || !ToolUtil::checkIsAdmin($admin['group_id'])){
            return ToolUtil::returnJson(400,'没有权限');
        }

        $param['remark'] = "删除附件文件";
        $file = DB::table('t_file')->where('id',$param['id'])->first(['id','type','file']);
        if(empty($file)){
            return ToolUtil::returnJson(300,'文件错误');
        }
        $param['details'] = ToolUtil::handleSuccessOrError($file);
        if(DB::table('t_file')->where('id',$param['id'])->delete()){
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }


    /**
     * 上传文件
     * @param $param
     * @return false|string
     */
    public static function uploadFile($param){

        return UploadUtil::upload_file($param['file'],$param);
    }


}